import requests
import re

url = 'https://www.kuaishou.com/graphql'

headers = {
    'content-type': 'application/json',
    'Cookie': 'kpf=PC_WEB; kpn=KUAISHOU_VISION; clientid=3; did=web_2dc28de9f976bf44abe9c0fc30e969cb; client_key=65890b29; didv=1656661637125; ktrace-context=1|MS43NjQ1ODM2OTgyODY2OTgyLjIxOTc0NzE2LjE2NTY2NjE4NjU3NjMuNzY4NjI=|MS43NjQ1ODM2OTgyODY2OTgyLjI2MjYzODQzLjE2NTY2NjE4NjU3NjMuNzY4NjM=|0|graphql-server|webservice|false|NA',
    'Host': 'www.kuaishou.com',
    'Origin': 'https://www.kuaishou.com',
    'Referer': 'https://www.kuaishou.com/search/video?searchKey=%E5%8F%98%E8%A3%85%E8%A7%86%E9%A2%91',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}
for page in range(1, 5):
    data = {
    'operationName': "visionSearchPhoto",
    'query': "fragment photoContent on PhotoEntity {\n  id\n  duration\n  caption\n  likeCount\n  viewCount\n  realLikeCount\n  coverUrl\n  photoUrl\n  photoH265Url\n  manifest\n  manifestH265\n  videoResource\n  coverUrls {\n    url\n    __typename\n  }\n  timestamp\n  expTag\n  animatedCoverUrl\n  distance\n  videoRatio\n  liked\n  stereoType\n  profileUserTopPhoto\n  __typename\n}\n\nfragment feedContent on Feed {\n  type\n  author {\n    id\n    name\n    headerUrl\n    following\n    headerUrls {\n      url\n      __typename\n    }\n    __typename\n  }\n  photo {\n    ...photoContent\n    __typename\n  }\n  canAddComment\n  llsid\n  status\n  currentPcursor\n  __typename\n}\n\nquery visionSearchPhoto($keyword: String, $pcursor: String, $searchSessionId: String, $page: String, $webPageArea: String) {\n  visionSearchPhoto(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId, page: $page, webPageArea: $webPageArea) {\n    result\n    llsid\n    webPageArea\n    feeds {\n      ...feedContent\n      __typename\n    }\n    searchSessionId\n    pcursor\n    aladdinBanner {\n      imgUrl\n      link\n      __typename\n    }\n    __typename\n  }\n}\n",
    'variables': {'keyword': "变装视频", 'pcursor': str(page), 'page': "search"}
    }
    # 发送请求
    resp = requests.post(url , json=data, headers=headers)

    # 获取数据
    json_data = resp.json()

    # 解析数据
    # print(json_data)
    feeds = json_data['data']['visionSearchPhoto']['feeds']
    for feed in feeds:
        caption = feed['photo']['caption'].split('@')[0]
        caption = re.sub(r'[\/:*?"<>|\n]', '', caption)   # 替换掉文件名中不能有的字符
        photoUrl = feed['photo']['photoUrl']
        print(caption, photoUrl)

    # print(len(caption))
        # 保存数据
        video_data = requests.get(url=photoUrl).content
        with open(f'video/{caption}.mp4', mode='wb') as f:
            f.write(video_data)


print('全部爬取成功！！！')












